<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        align="start">
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<script><![CDATA[
var is = window.opener.SimpleTest.is;
window.onerror = window.opener.onerror;

const anchorPositions =
  [ "before_start", "before_end", "after_start", "after_end",
    "start_before", "start_after", "end_before", "end_after", "overlap", "screen"];
var currentPosition;

function testResizer(resizerid, noShrink, hResize, vResize, testid)
{
  var rect = document.getElementById(resizerid + "-container").getBoundingClientRect();
  var resizer = document.getElementById(resizerid);
  var resizerrect = resizer.getBoundingClientRect();

  var originalX = resizerrect.left;
  var originalY = resizerrect.top;

  const scale = 20;
  for (var mouseX = -1; mouseX <= 1; ++mouseX) {
    for (var mouseY = -1; mouseY <= 1; ++mouseY) {
      var expectedWidth = rect.width + hResize * mouseX * scale;
      var expectedHeight = rect.height + vResize * mouseY * scale;

      if (noShrink) {
        if (mouseX == -1)
          expectedWidth = rect.width;
        if (mouseY == -1)
          expectedHeight = rect.height;
      }

      synthesizeMouse(document.documentElement, originalX + 5, originalY + 5, { type:"mousedown" });
      synthesizeMouse(document.documentElement, originalX + 5 + mouseX * scale,
                      originalY + 5 + mouseY * scale, { type:"mousemove" });

      var newrect = document.getElementById(resizerid + "-container").getBoundingClientRect();
      is(Math.round(newrect.width), Math.round(expectedWidth), "resize element " + resizerid +
         " " + testid + " width moving " + mouseX + "," + mouseY + ",,," + hResize);
      is(Math.round(newrect.height), Math.round(expectedHeight), "resize element " + resizerid +
         " " + testid + " height moving " + mouseX + "," + mouseY);
      // release
      synthesizeMouse(document.documentElement, originalX + 5 + mouseX * scale,
                      originalY + 5 + mouseY * scale, { type:"mouseup" });
      // return to the original size
      synthesizeMouse(document.documentElement, originalX + 5 + mouseX * scale,
                      originalY + 5 + mouseY * scale, { type:"dblclick" });
      var newrect = document.getElementById(resizerid + "-container").getBoundingClientRect();
      is(Math.round(newrect.width), Math.round(rect.width), "resize element " + resizerid +
         " " + testid + " doubleclicking to restore original size");
      is(Math.round(newrect.height), Math.round(rect.height), "resize element " + resizerid +
         " " + testid + " doubleclicking to restore original size");
    }
  }
}

function doTest() {
  // first, check if a resizer with a element attribute set to an element that
  // does not exist does not cause a problem
  var resizer = document.getElementById("notfound");
  synthesizeMouse(resizer, 5, 5, { type:"mousedown" });
  synthesizeMouse(resizer, 10, 10, { type:"mousemove" });
  synthesizeMouse(resizer, 5, 5, { type:"mouseup" });

  testResizer("outside", true, 1, 1, "");
  testResizer("html", true, 1, 1, "");
  testResizer("inside", true, 1, 1, "");
  testResizer("inside-large", false, 1, 1, "");
  testResizer("inside-with-border", true, 1, 1, "");

  document.getElementById("inside-popup-container").
    openPopupAtScreen(Math.ceil(window.mozInnerScreenX) + 100, Math.ceil(window.mozInnerScreenY) + 100);
}

function popupShown(event)
{
  testResizer("inside-popup", false, 1, 1, "");
  document.getElementById("inside-popup-container").id = "outside-popup-container";
  testResizer("outside-popup", false, 1, 1, "");

  var resizerrect = document.getElementById("inside-popup").getBoundingClientRect();
  synthesizeMouse(document.documentElement, resizerrect.left + 5, resizerrect.top + 5, { type:"mousedown" });
  synthesizeMouse(document.documentElement, resizerrect.left + 2000, resizerrect.top + 2000, { type:"mousemove" });

  var isMac = (navigator.platform.indexOf("Mac") >= 0);
  var popuprect = document.getElementById("outside-popup-container").getBoundingClientRect();
  // subtract 3 due to space left for panel dropshadow
  is(Math.ceil(window.mozInnerScreenX) + popuprect.right,
     (isMac ? screen.availLeft + screen.availWidth : screen.left + screen.width) - 3, "resized to edge width");
  is(Math.ceil(window.mozInnerScreenY) + popuprect.bottom,
     (isMac ? screen.availTop + screen.availHeight : screen.top + screen.height) - 3, "resized to edge height");

  resizerrect = document.getElementById("inside-popup").getBoundingClientRect();
  synthesizeMouse(document.documentElement, resizerrect.left + 5, resizerrect.top + 5, { type:"mouseup" });

  event.target.hidePopup();
}

function popupHidden()
{
  if (anchorPositions.length == 0) {
    window.close();
    window.opener.SimpleTest.finish();
    return;
  }

  currentPosition = anchorPositions.shift();
  var anchor = document.getElementById("anchor");
  var popup = document.getElementById("anchored-panel-container");

  if (currentPosition == "screen")
    popup.openPopupAtScreen(window.screenX + 100, window.screenY + 100);
  else
    popup.openPopup(anchor, currentPosition);
}

function anchoredPopupShown(event)
{
  var leftAllowed = (currentPosition.indexOf("end_") == -1 && currentPosition.indexOf("_start") == -1);
  var rightAllowed = (currentPosition.indexOf("start_") == -1 && currentPosition.indexOf("_end") == -1);
  var topAllowed = (currentPosition.indexOf("after_") == -1 && currentPosition.indexOf("_before") == -1);
  var bottomAllowed = (currentPosition.indexOf("before_") == -1 && currentPosition.indexOf("_after") == -1);

  if (currentPosition == "overlap") {
    leftAllowed = topAllowed = false;
    rightAllowed = bottomAllowed = true;
  }

  var resizerTypes = [ "topleft", "top", "topright", "left", "right",
                       "bottomleft", "bottom", "bottomright", "bottomend" ];
  for (var r = 0; r < resizerTypes.length; r++) {
    var resizerType = resizerTypes[r];
    var horiz = 0, vert = 0;
    if (leftAllowed && resizerType.indexOf("left") >= 0) horiz = -1;
    else if (rightAllowed && (resizerType.indexOf("right") >= 0 || resizerType == "bottomend")) horiz = 1;

    if (topAllowed && resizerType.indexOf("top") >= 0) vert = -1;
    else if (bottomAllowed && resizerType.indexOf("bottom") >= 0) vert = 1;

    document.getElementById("anchored-panel").dir = resizerType;
    testResizer("anchored-panel", false, horiz, vert, currentPosition + " " + resizerType);
  }

  event.target.hidePopup();
}

window.opener.SimpleTest.waitForFocus(doTest, window);
]]></script>

<resizer id="outside" dir="bottomend" element="outside-container"/>
<resizer id="notfound" dir="bottomend" element="nothing"/>
<hbox id="outside-container">
  <hbox minwidth="46" minheight="39"/>
</hbox>
<html:div id="html-container" xmlns:html="http://www.w3.org/1999/xhtml">
  <html:button>One</html:button><html:br/>
  <resizer id="html" dir="bottomend" element="_parent"/>
</html:div>
<hbox id="anchor" align="start" style="margin-left: 100px;">
  <hbox id="inside-container" align="start">
    <hbox minwidth="45" minheight="41"/>
    <resizer id="inside" dir="bottomend" element="_parent"/>
  </hbox>
  <hbox id="inside-large-container" width="70" height="70" align="start">
    <resizer id="inside-large" dir="bottomend" element="_parent"/>
  </hbox>
  <hbox id="inside-with-border-container" style="border: 5px solid red; padding: 2px; margin: 2px;" align="start">
    <hbox minwidth="35" minheight="30"/>
    <resizer id="inside-with-border" dir="bottomend" element="_parent"/>
  </hbox>
</hbox>

<panel id="inside-popup-container" align="start" onpopupshown="popupShown(event)" onpopuphidden="popupHidden()">
  <resizer id="inside-popup" dir="bottomend"/>
  <hbox width="50" height="50" flex="1"/>
</panel>
<resizer id="outside-popup" dir="bottomend" element="outside-popup-container"/>

<panel id="anchored-panel-container" align="start" onpopupshown="anchoredPopupShown(event)"
       onpopuphidden="popupHidden()">
  <hbox width="50" height="50" flex="1"/>
  <resizer id="anchored-panel" width="20" height="20"/>
</panel>

</window>
